/*
 * PesecNaKonciDialog.java
 *
 * Created on 2.12.2007, 17:30:09
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cz.possoft.client.chess.gui.components;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;

import cz.possoft.client.chess.gui.panel.ChessPanel;
import cz.possoft.client.gui.panel.nastavenispodnipanel.panel.NastaveniShowPanel;
import cz.possoft.client.utils.PanelHolderUtils;
import cz.possoft.messages.PropertiesManager;
import cz.possoft.messages.Text;
import cz.possoft.serializable.chess.figurky.*;


/**
 *
 * @author mara
 *
 * dialog se objevi kdyz pesec dojde na konec
 */
public class PesecNaKonciDialog extends JDialog {

    public static final int X1 = 235;
    public static final int Y1 = 250;
    public static final int WIDTH = 254;
    public static final int HEIGHT = 100;

    //
    private int color;
    private int xx;
    private int yy;
    // Aktualni instance Vyzivaciho dialogu. Vyzivaci dialog muze byt jen jeden
    private static PesecNaKonciDialog currentInstance;
    
    JButton queen;
    JButton tower;
    JButton rider;
    JButton shooter;

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public static PesecNaKonciDialog getCurrentInstance() {
        return currentInstance;
    }

    public static void setCurrentInstance(PesecNaKonciDialog currentInstance) {
        PesecNaKonciDialog.currentInstance = currentInstance;
    }

    public int getXx() {
        return xx;
    }

    public void setXx(int xx) {
        this.xx = xx;
    }

    public int getYy() {
        return yy;
    }

    public void setYy(int yy) {
        this.yy = yy;
    }

    public static PesecNaKonciDialog getInstance(int color, int x, int y) {
        JFrame frame = new JFrame(PropertiesManager.getInstance().getMessageForKey(Text.PESEC_NA_KONCI));
        frame.setBounds(X1, Y1, WIDTH, HEIGHT);
        PesecNaKonciDialog dialog = new PesecNaKonciDialog(frame, PropertiesManager.getInstance().getMessageForKey(Text.PESEC_NA_KONCI), true);
        PesecNaKonciDialog.currentInstance = dialog;
        dialog.setBounds(X1, Y1, WIDTH, HEIGHT);
        dialog.setColor(color);
        dialog.setXx(x);
        dialog.setYy(y);
        dialog.initContents();       
        dialog.setVisible(true);
        return dialog;
    }

 // Dialog se z externiho prostredi nevytvari pres konstruktor ale pres metodu getInstance
    private PesecNaKonciDialog() {} 
    
    private PesecNaKonciDialog(Frame frame, String title, boolean modal) {
        super(frame, title, modal);
    }

    // pri dispose u dialogu nastavime currentInstance na null.
    
    public void dispose() {
        super.dispose();
        currentInstance = null;
    }

    public void initContents() {
        Container panel = getContentPane();
        FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
        panel.setLayout(fl);

        // Pridavani komponent do hlavniho panelu
        String ret1 = (getColor() == 1) ? "black" : "white";
        String ret2 = ((getXx() + getColor()) % 2 == 1) ? "black" : "white";

        queen = new JButton(new ImageIcon("figurky/" + ret1 + "Queen-" + ret2 + ".GIF"));
        rider = new JButton(new ImageIcon("figurky/" + ret1 + "Rider-" + ret2 + ".GIF"));
        shooter = new JButton(new ImageIcon("figurky/" + ret1 + "Shooter-" + ret2 + ".GIF"));
        tower = new JButton(new ImageIcon("figurky/" + ret1 + "Tower-" + ret2 + ".GIF"));

        queen.setPreferredSize(new Dimension(55, 55));
        rider.setPreferredSize(new Dimension(55, 55));
        shooter.setPreferredSize(new Dimension(55, 55));
        tower.setPreferredSize(new Dimension(55, 55));

        queen.addActionListener(new FigListener("Queen"));
        rider.addActionListener(new FigListener("Rider"));
        tower.addActionListener(new FigListener("Tower"));
        shooter.addActionListener(new FigListener("Shooter"));

        panel.add(queen);
        panel.add(rider);
        panel.add(shooter);
        panel.add(tower);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        addWindowListener(new DialogCloseListener());
    }

// nastava pri stisku na tlacitko nektere figurky v dialogu
    private class FigListener implements ActionListener {

        private String figType;

        public FigListener(String figType) {
            this.figType = figType;
        }

        public void actionPerformed(ActionEvent ae) {
            NastaveniShowPanel.getConnectThread().sendToServer("VYBRANA_FIGURKA: " + getXx() + getYy() + getColor() + figType);
            ChessPanel chessPanel = (ChessPanel)PanelHolderUtils.getCurrentGamePanel();
            Figurka fig = chessPanel.setFigurkaFromPesecDosel(figType, getXx(), getYy(), getColor());
            chessPanel.controlSach(fig);
            PesecNaKonciDialog dialog = PesecNaKonciDialog.getCurrentInstance();
            dialog.dispose();
        }
    }

    private class DialogCloseListener extends WindowAdapter {

        // nastavime damu v pripade, ze hrac zavrel dialog        
        public void windowClosing(WindowEvent ae) {
            FigListener fl = new FigListener("Queen");
            fl.actionPerformed(new ActionEvent(queen, ActionEvent.ACTION_PERFORMED, null));
        }
    }
}
